package com.example.deliveryapplication.wiget

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.view.Menu
import android.view.View
import android.view.View.OnTouchListener
import android.view.WindowManager
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.PopupMenu
import com.example.deliveryapplication.*
import com.example.deliveryapplication.entity.SelectString
import kotlinx.android.synthetic.main.add_plan_dialog.*


class AddPlanDialog(context: Context): Dialog(context) {
//    private var companyNameTV:TextView?=null
//    private var mConfirmBTN:Button?=null
    private var listener:CallBack?=null
    private var typeList:List<SelectString>?=null
    private var clientList:List<SelectString>?=null
    private var cuList:List<SelectString>?=null
    private var lastSelect=-1
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.add_plan_dialog)
        val lp = window?.attributes
        // 设置窗口宽度为屏幕宽度的80%
        val displayMetrics = context.resources.displayMetrics
        lp?.width = (displayMetrics.widthPixels * 0.85).toInt()
        lp?.height = WindowManager.LayoutParams.WRAP_CONTENT
        lp?.gravity = Gravity.CENTER
        window?.attributes = lp
        mConfirmBTN.setOnClickListener {
            val name=companyNameTV.text.toString()
            val remark=remarkTV.text.toString()
            if(name.isNullOrEmpty()){
               context.showToast("请先输入计划数量")
            }else{
                if(bearTypeSP.selectedItem==null){

                }else{
                    val bean=bearTypeSP.selectedItem as SelectString
                    val bean2=clientSP.selectedItem as SelectString
                    val cu=cuSpinner.selectedItem as SelectString?
                    listener?.onConfirmClick(name,bean,bean2,remark,cu?.name.handlerNull())
                    dismiss()
                }
            }
        }
        typeList?.let {
            val adapter = StringListSpinnerAdapter(context, it)
            bearTypeSP.adapter = adapter
        }
        clientList?.let {
            val adapter=StringListSpinnerAdapter(context,it)
            clientSP.adapter=adapter
        }
        cuList?.let {
//            if(it.isNotEmpty()){
//                cuEditText.setText(it[0].handlerNull())
//            }
            val adapter = StringListSpinnerAdapter(context,it)
            cuSpinner.adapter=adapter
            //cuSpinner.adapter=adapter


        }

//        cuSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
//            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
//                val selectedItem = parent?.getItemAtPosition(position).toString()
//                cuEditText.setText(selectedItem)
//                lastSelect=position
//            }
//
//            override fun onNothingSelected(parent: AdapterView<*>?) {
//                // 当没有选择项时的处理逻辑
//                Log.d("测试","有没有做些什么")
//            }
//        }


    }
    fun setTypeList(list:List<SelectString>){
        this.typeList=list
    }
    fun setClientList(list:List<SelectString>){
        this.clientList=list
    }
    fun setCUList(list:List<SelectString>){
        this.cuList=list
    }
    fun setListener(listener:CallBack){
        this.listener=listener
    }
    interface CallBack{
        fun onConfirmClick(name:String,data1:SelectString,data2:SelectString,remark:String,cu:String)
    }
}
